var dataStr = d.data.id;
var config = d.colDef.kaconfig;
var uuid = ka.uuidStr();
var index = layui.layer.open({
    type: 1, // page 层类型
    area: ['537px', '60%'],
    title: `
    <i class="icon iconfont icon-mail" style="margin-top: 10px;text-align: center;width: 50px;height: 50px;line-height: 50px;font-size: 30px;float: left;position: absolute;background-color: #f3f1ff;border-radius: 50%;"></i>
    <div style="font-size: 18px;font-weight: bold;margin-left: 70px;margin-top: 5px;position: absolute;">编辑表单</div>
    <div style="color: #989898;margin-top: 35px;margin-left: 70px;font-size: 12px;position: absolute;">用于全局表单添加编辑</div>
    `,
    shade: [0.6, '#FFF'], // 点击遮罩区域，关闭弹层
    maxmin: false, // 允许全屏最小化
    anim: 0, // 0-6 的动画形式，-1 不开启
    content: `
    <input type="hidden" id="id` + uuid + `" name="id" value="" placeholder="Id" autocomplete="off" class="layui-input">
    <div style="padding-left: 15px;padding-right: 15px;padding-top: 10px;">
        <form id="` + uuid + `_form" class="layui-form layui-form-pane" action="" lay-filter="` + uuid + `_form" style="display: block;margin-top: 25px;">
            <div class="layui-form-item">
                <label class="layui-form-label">表单名称:</label>
                <div class="layui-input-block">
                    <input type="text" id="name` + uuid + `" name="name" value="" placeholder="请输入表单名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">类型:</label>
                <div class="layui-input-block">
                    <select lay-search="" lay-filter="type` + uuid + `" id="type` + uuid + `" name="type" value="">
                        <option value="2">流程表格</option>
                        <option value="1">表格按钮事件</option>
                    </select>
                </div>
            </div>
            <div id="` + uuid + `_form_hiden" class="layui-form-item">
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">内容:</label>
                <div class="layui-input-block">
                    <textarea id="remark` + uuid + `" name="remark" placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
            </div>
        </form>
        <div>
            <a class="layui-btn layui-btn-sm" uurid="` + uuid + `" id="formDesignerGridAddId_btn" style="position: absolute;bottom: 10px;right: 12px;">确定修改</a>
        </div>
    </div>
    `,
    success: function () {
        layui.form.render($('#' + uuid + "_form"));
        layui.form.on('select(type'+ uuid +')', function (data) {
            var value = data.value; // 获得被选中的值
            $('#' + uuid + "_form_hiden").html("");
            var fv = [];
            var option = kaValue.getUrl("getGridList");
            option.url = option.url;
            option.success = function (re) {
                if (re.code == 200) {
                    fv = re.data;
                }
            }
            ka.ajax(option);
            if(value == "1"){
                var optionHtml = ``;
                for(var i in fv){
                    optionHtml = optionHtml + `<option id="` + fv[i].id + `_` + fv[i].code + `" gridIdStr="` + fv[i].id + `" gridCode="` + fv[i].code + `" gridName="` + fv[i].name + `"  value="` + fv[i].id + `_` + fv[i].code + `">` + fv[i].name + `</option>`;
                }
                var form_hiden = `
                <label class="layui-form-label">表格:</label>
                <div class="layui-input-block">
                    <select lay-search="" id="gridId` + uuid + `" name="gridId" value="">
                        ` + optionHtml + `
                    </select>
                </div>
                `;
                $('#' + uuid + "_form_hiden").html(form_hiden);
                layui.form.render($('#' + uuid + "_form"));
            }
        });
        var srd = {};
        var option = kaValue.getUrl("formDesignerGetId");
        option.async = false;
        option.url = option.url + "/" + dataStr;
        option.success = function (re) {
            if (re.code == 200) {
                srd = re.data;
            }
        }
        ka.ajax(option);
        if(srd.type == 1){
            var fv = [];
            var option = kaValue.getUrl("getGridList");
            option.url = option.url;
            option.success = function (re) {
                if (re.code == 200) {
                    fv = re.data;
                }
            }
            ka.ajax(option);
            var optionHtml = ``;
            for(var i in fv){
                optionHtml = optionHtml + `<option id="` + fv[i].id + `_` + fv[i].code + `" gridIdStr="` + fv[i].id + `" gridCode="` + fv[i].code + `" gridName="` + fv[i].name + `"  value="` + fv[i].id + `_` + fv[i].code + `">` + fv[i].name + `</option>`;
            }
            var form_hiden = `
            <label class="layui-form-label">表格:</label>
            <div class="layui-input-block">
                <select lay-search="" id="gridId` + uuid + `" name="gridIdStr" value="">
                    ` + optionHtml + `
                </select>
            </div>
            `;
            $('#' + uuid + "_form_hiden").html(form_hiden);
            layui.form.render($('#' + uuid + "_form"));
        }
        srd["gridIdStr"] = srd.gridId + "_" + srd.gridCode;
        layui.form.val(uuid + `_form`, srd);
        $("#formDesignerGridAddId_btn").click(function () {
            var uurid = $(this).attr("uurid");
            var name = $("#name" + uurid).val();
            var remark = $("#remark" + uurid).val();
            if (name == undefined || name == null || name == "") {
                kaMsg.error({ "code": "500", "msg": "表单名称不能为空！" });
                return false;
            }
            var type = $("#type" + uurid).val();
            if (type == undefined || type == null || type == "") {
                kaMsg.error({ "code": "500", "msg": "类型不能为空！" });
                return false;
            }
            var gridIdStr = ""; 
            var gridCode = "";
            var gridName = "";
            if(type == 1){
                var gridId = $("#gridId" + uurid).val();
                if (gridId == undefined || gridId == null || gridId == "") {
                    kaMsg.error({ "code": "500", "msg": "表格不能为空！" });
                    return false;
                }
                gridIdStr = $("#" + gridId).attr("gridIdStr"); 
                gridCode = $("#" + gridId).attr("gridCode");
                gridName = $("#" + gridId).attr("gridName");
            }
            var option = kaValue.getUrl("formDesignerEdit");
            option.async = false;
            option.data = {
                "id": dataStr,
                "name": name,
                "type": type,
                "gridId": gridIdStr,
                "gridCode": gridCode,
                "gridName": gridName,
                "remark": remark
            }
            option.success = function (re) {
                if (re.code == 200) {
                    layer.close(index);
                    kagrid.getData(config, function (reData, config) {
                        var gridApi = kagrid.getGridApi(config.id);
                        gridApi.setRowData(reData);
                        kagrid.playUpSort(config);
                        kaMsg.success(re);
                    })
                } else {
                    kaMsg.error({ "code": re.code, "msg": re.msg });
                }
            }
            ka.ajax(option);
        })
    }
});